# 1.找到未加密的参数：params、encSecKey
# 2.找到加密方法，把参数进行加密 params -> encText、encSecKey -> encSecKey
# 3.发送请求

import requests
from lxml import etree
from Crypto.Cipher import AES
from base64 import b64encode
import json

url = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token='

# 找到真实参数
data = {
    "csrf_token": "",
    "cursor": "-1",
    "offset": "0",
    "orderType": "1",
    "pageNo": "1",
    "pageSize": "20",
    "rid": "R_SO_4_1325905146",
    "threadId": "R_SO_4_1325905146"
}

# 加密过程 window.asrsea
# var bMr1x = window.asrsea(JSON.stringify(i0x), bsg8Y(["流泪", "强"]), bsg8Y(TH5M.md), bsg8Y(["爱心", "女孩", "惊恐", "大笑"]));

# window.asrsea(JSON.stringify(i0x), bsg8Y(["流泪", "强"]), bsg8Y(TH5M.md), bsg8Y(["爱心", "女孩", "惊恐", "大笑"])) = d(d, e, f, g)
# 参数解析
# d: 数据 = JSON.stringify(i0x)
# e: bsg8Y(["流泪", "强"]) = '010001'
# f: bsg8Y(TH5M.md) = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
# g: bsg8Y(["爱心", "女孩", "惊恐", "大笑"]) = '0CoJUm6Qyw8W8jud'

e = '010001'
f = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
g = '0CoJUm6Qyw8W8jud'

# 设置固定值 ""
i = "aQZBk48S4G5Ssl5f"
def getEncSecKey():
    return "a54e0db88cd31cf050e7961fe757ce932ccdfcca66141656e8bb215422dd89798c98b1143ff2cfae36da1934de5304404c5e8be1ca5aa577dfac091c1abd109271990cf7ddd53fa8651bb207e355482eee04a7b13e0d47792f141fa17cdcabab9da387ff6eaaba0d445982737c0c34fa7ffd4d2506453a2e905fbdcf9a9c9816"

# 默认 data 为字符串
def getEncText(data):
    first = encParams(data, g)
    second = encParams(first, i)
    return second

def to_16(data):
    pad = 16 - len(data) % 16
    data += chr(pad) * pad
    return data

def encParams(data, key):
    iv = "0102030405060708"
    data = to_16(data)
    aes = AES.new(key=key.encode('utf-8'), IV=iv.encode('utf-8'), mode=AES.MODE_CBC)
    bs = aes.encrypt(data.encode('utf-8'))
    return str(b64encode(bs), 'utf-8')

"""
!function() {
    # 返回参数 a 位随机字符串
    function a(a) { 
        var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
        for (d = 0; a > d; d += 1)
            e = Math.random() * b.length,
            e = Math.floor(e),
            c += b.charAt(e);
        return c
    }
    
    function b(a, b) {
        var c = CryptoJS.enc.Utf8.parse(b)
          , d = CryptoJS.enc.Utf8.parse("0102030405060708")
          , e = CryptoJS.enc.Utf8.parse(a)
          , f = CryptoJS.AES.encrypt(e, c, {
            iv: d,                      # 偏移量
            mode: CryptoJS.mode.CBC     # 加密模式
        });
        return f.toString()
    }
    
    # 参数 a 是随机值
    # 参数 b、c 是固定值
    function c(a, b, c) {
        var d, e;
        return setMaxDigits(131),
        d = new RSAKeyPair(b,"",c),
        e = encryptedString(d, a)
    }
    function d(d, e, f, g) {
        var h = {}
          , i = a(16);
        # 第一次加密：数据 d 和定值 g
        h.encText = b(d, g),
        
        # 第二次加密：第一次加密结果和随机值 i
        h.encText = b(h.encText, i),
        
        # i 是随机值，e、f 是定值
        # 把 i 固定则 encSecKey 为定值
        h.encSecKey = c(i, e, f),
        return h
    }
    function e(a, b, d, e) {
        var f = {};
        return f.encText = c(a + e, b, d),
        f
    }
    window.asrsea = d,
    window.ecnonasr = e
}();
"""

resp = requests.post(url=url, data={
    "params": getEncText(json.dumps(data)),
    "encSecKey": getEncSecKey()
})
resp.encoding = 'utf-8'
print(resp.text)
